﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    /// <summary>
    /// 外送员操作类
    /// </summary>
    public class EmployeeDAO : Repository<Model.Entities.Model_Employee>, IEmployeeDAO
    {
        public override bool Add(Model.Entities.Model_Employee entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_Employee entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_Employee employee = GetByID(id);
            return Delete(employee);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from e in context.Employees
                                  where listID.Contains(e.ID) && e.IsUse==true&&e.IsDelete==false
                                  select e;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_Employee>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            Model.Entities.Model_Employee employee = GetByID(id);
            return employee == null ? false : true;
        }

        public override Model.Entities.Model_Employee GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Employees.FirstOrDefault(e => e.ID == id && e.IsUse == true && e.IsDelete == false);
            }
        }
        public override IList<Model.Entities.Model_Employee> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_Employee entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }
        /// <summary>
        /// 获取某一个店铺下面所有的员工列表
        /// </summary>
        /// <param name="dianPuID"></param>
        /// <returns></returns>
        public List<Model.Entities.Model_Employee> GetAllEmployeesByDianPuID(string dianPuID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                List<Model.Entities.Model_Employee> employees = new List<Model.Entities.Model_Employee>();
                return context.Employees.Where(e => e.DianPuID == dianPuID && e.IsUse == true && e.IsDelete == false).ToList();
            }
        }


        public Model.Entities.Model_Employee GetByName(string employeeName)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Employees.Where(e => e.LoginName == employeeName && e.IsDelete == false && e.IsUse == true).FirstOrDefault();
            }
        }


        public Model.Entities.Model_Employee GetByNameAndPassword(string name, string password)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Employees.FirstOrDefault(a => a.LoginName.Equals(name, StringComparison.CurrentCultureIgnoreCase) && a.LoginPassword.Equals(password, StringComparison.CurrentCultureIgnoreCase) && a.IsUse == true && a.IsDelete == false);
            }
        }
    }
}
